<style>
    #obj-update {
        padding: 20px 25px 25px 0;
    }

    #obj-update .layui-treeSelect .ztree li a, .ztree li span {
        margin: 0 0 2px 3px !important;
    }
</style>
<div class="layui-fluid" id="obj-update">
    <form class="layui-form" action="" lay-filter="obj-update-form">
        <div class="layui-form-item febs-hide">
            <label class="layui-form-label febs-form-item-require">记录id：</label>
            <div class="layui-input-block">
                <input type="text" name="id" data-th-value="${obj.id}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">图标标题：</label>
            <div class="layui-input-block">
                <input type="text"  name="iconTitle" autocomplete="off" class="layui-input" lay-verify="required" >
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">别名：</label>
            <div class="layui-input-block">
                <input type="text" lay-verify="required" name="iconName" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item febs-hide">
            <label class="layui-form-label febs-form-item-require">图标数据：</label>
            <div class="layui-input-block">
                <input type="text" name="iconData" id="iconData" >
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">图标：</label>
            <div class="layui-input-block">
                <div class="layui-upload">
                    <button type="button" class=" col-sm-4 layui-btn layui-btn-normal layui-btn-radius" id="changeIcon">更换图标</button>
                    <div class="layui-upload-list">
                        <img  class="col-sm-4 layui-upload-img" id="showPhoto" width="60px" height="60px">
                        <p id="showText"></p>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item febs-hide">
            <button class="layui-btn" lay-submit="" lay-filter="obj-update-form-submit" id="submit"></button>
        </div>
    </form>
</div>

<script data-th-inline="javascript">
    layui.use(['febs', 'form', 'formSelects', 'validate', 'treeSelect','upload'], function () {
        var $ = layui.$,
            febs = layui.febs,
            layer = layui.layer,
            form = layui.form,
            upload = layui.upload,
            obj = [[${obj}]],
            $view = $('#obj-update'),
            validate = layui.validate;

        form.verify(validate);
        form.render();

        initobjValue();
        //先让图标回显
        showPicture("showPhoto",obj.id);

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#changeIcon'
            , url: '/icon/iconUpload' //改成您自己的上传接口
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#showPhoto').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.code != 200) {
                    return layer.msg('上传失败');
                }
                //上传成功
                $("#iconData").val(res.data)
                var demoText = $('#showText');
                demoText.append('<span style="color: green;">上传成功</span>');
                return layer.msg('上传成功');
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#showText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        })

        function showPicture(imgId,iconId) {
            $.ajax({
                type: "GET",
                url:'/icon/iconShow?id=' + iconId,
                data: {},
                success: function(result) {
                    //ajax调用成功后返回的是图片的base64数据
                    $('#'+imgId).attr("src",result);
                }
            })



        }

        function initobjValue() {
            form.val("obj-update-form", {
                "id": obj.id,
                "iconTitle": obj.iconTitle,
                "iconName": obj.iconName,
            });
        }

        form.on('submit(obj-update-form-submit)', function (data) {
            if (febs.nativeEqual(data.field, obj)) {
                febs.alert.warn('数据未作任何修改！');
                return false;
            }
            febs.post(ctx + 'icon/update', data.field, function () {
                layer.closeAll();
                febs.alert.success('数据修改成功');
                $('#febs-obj').find('#query').click();
            });
            return false;
        });
    });
</script>